package cuttings;

import javax.swing.ImageIcon;
import java.util.Random;
import java.util.ArrayList;
import java.util.Iterator;
import java.awt.Rectangle;


public class Opponent extends Sprite {

    private Weapon weapon;
    private String opponent_name;
    private Integer opponent_ID;
    private ArrayList words;    

    public Opponent(String n) {
        this.x = 0;
        this.y = 0;  
        
        words = new ArrayList();
        opponent_name = randomWord();
        
        //weapon = new Weapon(x, y);
        //words.add(weapon);
        
        opponent_name = n;
        
		//opponent_ID = Min + (int)(Math.random() * ((Max - Min) + 1));
		opponent_ID = 0;

    }

    public void act(int direction) {
        this.x += direction;
    }
    
    public void addWord(String w) {
		Weapon newWord = new Weapon(w);
		words.add(newWord);		
	}
	
	public ArrayList getWords() {
		return words;
	}

    public Weapon getWeapon() {
        return weapon;
    }
    
    public void setName(String s) {
		opponent_name = s;
	}
	
	
	public String getName() {
		return opponent_name;
	}
	
	public Integer getID() {
		return opponent_ID;
	}
	
	public String randomWord()
	{
		String word;
		Random rng = new Random();    
		char[] text = new char[5];
		String characters = "abcdefghijklmnopqrstuvxwyz";
		for (int i = 0; i < 5; i++)
		{
			text[i] = characters.charAt(rng.nextInt(characters.length()));
		}
		word = String.valueOf(text);      
		
		return word;
	}       	

    public class Weapon extends Sprite {

        private boolean destroyed;
        private String word;
        private String originalWord;
        
        public Weapon(String s) {
            setDestroyed(false);
			int Min = 240;
			int Max = 740;

			this.x = Min + (int)(Math.random() * ((Max - Min) + 1));
            this.y = 60;
            
			word = s;
			originalWord = s;
          
          //  ImageIcon ii = new ImageIcon(this.getClass().getResource(weapon));
           // setImage(ii.getImage());
        }        


        public void setDestroyed(boolean destroyed) {
            this.destroyed = destroyed;
        }

        public boolean isDestroyed() {
            return destroyed;
        }
        
        public String getWord() {
			return word;
		}
		
        public String getOriginalWord() {
			return originalWord;
		}		
		
		public int removeLetter(char c) {
			 String r = "";
			 int ct=0;
			 if(word.length() == 0) return 0;
			
			for (int i = 0; i < word.length(); i ++) {
				if (word.charAt(i) != c) r += word.charAt(i);
				else
				{
					if(ct>=1) r += word.charAt(i);
					ct++;
				}
			}
			
			word = r;
			
			if(word.length() == 0) return 10;
			else if(ct>0) return 1;
			else return 0;
		}
		public Rectangle getBounds() {
			return new Rectangle(x, y, word.length()*10, 20);
		}			
    }
}
